import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_page/mine_business/mine_setting/mine_setting_profile_page.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';
import 'package:xvideos/hj_utils/global.dart';
import 'package:xvideos/tools_base/base_list_controller.dart';
import 'package:xvideos/tools_base/global_store/store.dart';
import 'package:xvideos/tools_base/loading/LoadingHelper.dart';

import 'setting_profile_widget.dart';

abstract class MineSettingProfilePage extends BaseListController {
  final title = '';

  @override
  fetchPageData({bool isRefresh = true, bool needLoading = false}) {}

  late final nickNameTfCtr = TextEditingController();
  late final sloganTfCtr = TextEditingController();

  onSaveProfile();
}

class SettingNickNameController extends MineSettingProfilePage {
  @override
  String get title => '修改昵称';

  @override
  onSaveProfile() async {
    if (nickNameTfCtr.text.isEmpty) {
      showToast('昵称不能为空～');
      return;
    }
    LoadingHelper.showLoading();
    final res = await MineService.updateUserInfo({'name': nickNameTfCtr.text});
    LoadingHelper.dismissLoading();
    if (res) {
      globalStore.updateUserInfo();
      Get.back();
    }
  }

  @override
  instanceChildItem(int index) {
    return SettingNickName();
  }
}

class SettingSloganController extends MineSettingProfilePage {
  @override
  String get title => '个性签名';

  @override
  onSaveProfile() async {
    if (sloganTfCtr.text.isEmpty) {
      showToast('请输入个性签名～');
      return;
    }
    LoadingHelper.showLoading();
    final res = await MineService.updateUserInfo({'summary': sloganTfCtr.text});
    LoadingHelper.dismissLoading();
    if (res) {
      globalStore.updateUserInfo();
      Get.back();
    }
  }

  @override
  instanceChildItem(int index) {
    return SettingSlogan();
  }
}

MineSettingProfilePage instanceSettingProfileController(SettingProfileType type) {
  switch (type) {
    case SettingProfileType.nickName:
      return SettingNickNameController();
    case SettingProfileType.slogan:
      return SettingSloganController();
    default:
      throw '';
  }
}
